#include "stdafx.h"
#include "TouchScreenWin32.h"
#include "Application.h"
#include "Game/Level/Level.h"
#include "Game/Camera/GameCamera.h"
#include "Lib2D/UIInfo.h"

// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Ctors / Dtor
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TouchScreenWin32::TouchScreenWin32(irr::core::rect<short> rct)
{
	m_rect = rct;
}

TouchScreenWin32::~TouchScreenWin32()
{
}


// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Methods (irr:IEventReceiver impl.)
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TouchScreenWin32::OnEvent(const irr::SEvent& ev)
{
	if( Application::instance()->IsCheatMode()  || Application::instance()->IsFreeCameraMode())
	{
		if(CLevel::hasInstance())
		{
			CGameCamera* pCamera= CLevel::instance()->GetCamera();
			if( pCamera )
			{
				pCamera->OnEvent( ev );
			}
		}
	}

	static bool isDown = false;

	if (!m_rect.isPointInside(irr::core::position2d<short>(ev.MouseInput.X, ev.MouseInput.Y)))
	{
		if(ev.EventType == irr::EET_MOUSE_INPUT_EVENT)
		{
			switch(ev.MouseInput.Event)
			{
			case irr::EMIE_MOUSE_MOVED:				
				if( isDown )
				{
					this->touchEnded(TouchPoint(ev.MouseInput.X, ev.MouseInput.Y), 1);
				}
				isDown = false;
				break;
			}
		}
		return false;
	}

	if(ev.EventType == irr::EET_MOUSE_INPUT_EVENT)
	{		
		switch(ev.MouseInput.Event)
		{
		case irr::EMIE_LMOUSE_PRESSED_DOWN:
			{
			isDown = true;
			int _1v1_x = UIInfo::instance()->GetUI_SCALED_2_ORI_X(ev.MouseInput.X);
			int _1v1_y = UIInfo::instance()->GetUI_SCALED_2_ORI_Y(ev.MouseInput.Y);
			this->touchBegan(TouchPoint(_1v1_x, _1v1_y), 1,false);
			return true;
			}
		case irr::EMIE_LMOUSE_LEFT_UP:
			{
			int _1v1_x = UIInfo::instance()->GetUI_SCALED_2_ORI_X(ev.MouseInput.X);
			int _1v1_y = UIInfo::instance()->GetUI_SCALED_2_ORI_Y(ev.MouseInput.Y);
			isDown = false;
			this->touchEnded(TouchPoint(_1v1_x, _1v1_y), 1);
			return true;
			}
		case irr::EMIE_MOUSE_MOVED:
			if(isDown)
			{
				int _1v1_x = UIInfo::instance()->GetUI_SCALED_2_ORI_X(ev.MouseInput.X);
				int _1v1_y = UIInfo::instance()->GetUI_SCALED_2_ORI_Y(ev.MouseInput.Y);
				this->touchMoved(TouchPoint(_1v1_x, _1v1_y), 1);
				return true;
			}
		}
	}

	return false;
}